package com.js.lib_common.mvp.presenter;


import com.js.lib_common.base.BasePresenter;
import com.js.lib_common.http.ApiCallback;
import com.js.lib_common.mvp.model.CommonBean;
import com.js.lib_common.mvp.model.response.BindBean;
import com.js.lib_common.mvp.model.response.MachineIdInfoResponse;
import com.js.lib_common.mvp.model.response.MaterialIdInfoResponse;
import com.js.lib_common.mvp.view.CheckProductView;


public class CheckProductPresenter extends BasePresenter<CheckProductView> {
    public CheckProductPresenter(CheckProductView view) {
        attachView(view);
    }


    public void check(String userId, String itemId, boolean isPassTest, String remark) {
        if (userId==null||userId.isEmpty()){
            mvpView.onFailed("userId 为空");
            return;
        }
        mvpView.showLoading();
        addSubscription(apiStores.check(userId,itemId,isPassTest?1:0,remark),
                new ApiCallback<CommonBean>() {

                    @Override
                    public void onSuccess(CommonBean response) {
                        if (response.isSuccess()) {
                            mvpView.onSuccess();
                        } else {
                            mvpView.onFailed(response.getMsg());
                        }

                    }

                    @Override
                    public void onFailure(String msg) {
                        mvpView.onFailed(String.format("check fail %s", msg));
                    }

                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }
                });
    }





    public void checkMachineId(String userId, final String machineId) {
        if (userId == null || userId.isEmpty()) {
            mvpView.onFailed("userId 为空");
            return;
        }
        mvpView.showLoading();
        addSubscription(apiStores.checkMachineId(userId, machineId),
                new ApiCallback<CommonBean<MachineIdInfoResponse>>() {
                    @Override
                    public void onSuccess(CommonBean<MachineIdInfoResponse> response) {
                        if (response.isSuccess()) {
                            BindBean bean=new BindBean();
                            bean.setQrcode(machineId);
                            bean.setType(BindBean.REQUEST_CODE_CHECK_MACHINEID);
                            bean.setInfo("检测,整机码");
                            bean.setMachineIdInfo(response.getObj());
                            mvpView.onSuccess(bean);
                        } else {
                            mvpView.onFailed(String.format("%s", response.getMsg()));
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        mvpView.onFailed(String.format("%s", msg));
                    }

                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }
                });

    }

    public void checkMaterialId(String userId, final String materialId) {
        if (userId == null || userId.isEmpty()) {
            mvpView.onFailed("userId 为空");
            return;
        }
        mvpView.showLoading();
        addSubscription(apiStores.checkMaterialId(userId, materialId),
                new ApiCallback<CommonBean<MaterialIdInfoResponse>>() {
                    @Override
                    public void onSuccess(CommonBean<MaterialIdInfoResponse> response) {
                        if (response.isSuccess()) {
                            BindBean bean=new BindBean();
                            bean.setQrcode(materialId);
                            bean.setType(BindBean.REQUEST_CODE_CHECK_MATERIALID);
                            bean.setInfo("检测,物料码");
                            bean.setMaterialIdInfo(response.getObj());
                            mvpView.onSuccess(bean);
                        } else {
                            mvpView.onFailed(String.format("%s", response.getMsg()));
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        mvpView.onFailed(String.format("%s", msg));
                    }

                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }
                });

    }

}
